﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using BLL;
using Utils;
public partial class OpenRootFace : System.Web.UI.Page
{
    //生成数据库的实例

    IBizSQL bizSQL = new BizSQL();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["user"] == null)
        {
            JSUtility.Alert(this,"您没有登录");
            Response.End();
        }
        else        
        {
            User user = Session["user"] as User;
           string account= user.Account;
           if (account != "root" && account != "jimxiao" && account != "xiaoshihe")
            {
                JSUtility.Alert(this,"您没有权限操作！");
                Response.End();
            }
        }

        if (!IsPostBack)
        {
            Create_MainMenu_TreeView();
        }
    }
    private void Create_MainMenu_TreeView()
    {
        MainMenuTreeView.Nodes.Clear();
        //取出菜单数据
        string strSQL = "select MainMenuID,ParentID,MenuName,LinkUrl from Sys_MainMenu order by AbsIndex DESC";
        DataSet ds = bizSQL.ExecSQL2(strSQL);
        if (ds == null)
        {
            return;
        }
        else
        {
            this.ViewState["ds"] = ds;

            //调用递归函数，完成树形结构的生成
            AddTree(0, (TreeNode)null);
        }
    }
    //递归添加树的节点
    private void AddTree(int ParentID, TreeNode pNode)
    {
        DataSet ds = (DataSet)this.ViewState["ds"];
        DataView dvTree = new DataView(ds.Tables[0]);
        //过滤ParentID,得到当前的所有子节点
        dvTree.RowFilter = "[ParentID] = " + ParentID;

        foreach (DataRowView Row in dvTree)
        {
            TreeNode Node = new TreeNode();

            if (pNode == null)
            {    //添加根节点

                Node.Text = Row["MenuName"].ToString();
                Node.Value = Row["MainMenuID"].ToString();
                
                Node.NavigateUrl = Row["LinkUrl"].ToString();
                Node.Target = "ifrmRoot";
                MainMenuTreeView.Nodes.Add(Node);                
               // Node.Expanded = true;
                AddTree(Int32.Parse(Row["MainMenuID"].ToString()), Node);    //再次递归
            }
            else
            {   //̀添加当前节点的子节点
                Node.Text = Row["MenuName"].ToString();
                Node.Value = Row["MainMenuID"].ToString();
                
                Node.NavigateUrl = Row["LinkUrl"].ToString();
                Node.Target = "ifrmRoot";
                pNode.ChildNodes.Add(Node);
              //  Node.Expanded = true;
                AddTree(Int32.Parse(Row["MainMenuID"].ToString()), Node);     //再次递归
            }
        }
    }
}
